<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>

            <a-form-item label="所属牧场:" name="ranchId">
              <a-select
                show-search
                v-model:value="where.ranchId"
                placeholder="请选择所属牧场"
                style="width: 205px;"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                @change="selectRanch"
              >
                <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                  {{ item.ranchName }}
                </a-select-option>
              </a-select>
            </a-form-item>


            <a-form-item label="所属牛舍:" name="oxtallId">
              <a-select
                show-search
                v-model:value="where.oxtallId"
                placeholder="请选择所属牛舍"
                style="width: 200px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                allow-clear
              >
                <a-select-option v-for="item in oxtallList" :key="item.oxtallId" >
                  {{ item.oxtallName }}
                </a-select-option>
              </a-select>
            </a-form-item>



            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>
    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="groupId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >

          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space v-permission="['MANAGERDEVICE_SPRAYCAMERAGROUP_ADD_BUTTON']">
              <a-button type="primary" @click="openEdit()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建</span>
              </a-button>
            </a-space>
          </template>

          <!-- table操作栏按钮 -->
          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a v-permission="['MANAGERDEVICE_SPRAYCAMERAGROUP_UPDATE_BUTTON']" @click="openEdit(record)">修改</a>
                <a-divider type="vertical" v-permission="['MANAGERDEVICE_SPRAYCAMERAGROUP_DEL_BUTTON']" />
                <div v-permission="['MANAGERDEVICE_SPRAYCAMERAGROUP_DEL_BUTTON']">
                  <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                    <a class="ele-text-danger">删除</a>
                  </a-popconfirm>
                </div>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>

    <!-- 编辑弹窗 -->
    <DeviceManagerdeviceCameraGroupEdit v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload"  />
  </div>
</template>

<script>
import { PlusOutlined } from '@ant-design/icons-vue';
import DeviceManagerdeviceCameraGroupEdit from './camera-group-edit.vue'
import {DeviceSprayCameraGroupApi} from "@/api/property/spraycamera/DeviceSprayCameraGroupApi";
import { RanchApi } from '@/api/property/RanchApi';
import { OxtallApi } from '@/api/property/OxtallApi';
import { message } from 'ant-design-vue';
export default {
  name: 'DeviceManagerdeviceCameraGroup',
  components: {
    PlusOutlined,
    DeviceManagerdeviceCameraGroupEdit
  },
  async mounted() {
  },
  async created() {
    let that = this;
    RanchApi.list({}).then(resp => {
      let _ranchList = (resp ? resp.data : null) || [];
      this.ranchList = _ranchList;
      if (_ranchList.length > 0) {
        OxtallApi.list({"orgId" : _ranchList[0].ranchId}).then(result => {
          this.oxtallList = (result ? result.data : null) || [];
        }).catch((err) => {
          console.log(err)
        });
      }
    });
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: 'ID',
          dataIndex: 'groupId'
        },
        {
          title: '区名字',
          dataIndex: 'groupName'
        },
        {
          title: '牧场',
          dataIndex: 'ranchName'
        },
        {
          title: '牛舍',
          dataIndex: 'oxtallName'
        },
        {
          title: '描述',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center',
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      ranchList : [],
      oxtallList : [],
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2025/03/08 10:06
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2025/03/08 10:06
     */
    reset() {
      this.where.oxtallId = undefined;
      this.where.ranchId = undefined;
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2025/03/08 10:06
     */
    async remove(row) {
      const result = await DeviceSprayCameraGroupApi.delete({ groupId: row.groupId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2025/03/08 10:06
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return DeviceSprayCameraGroupApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    },
    selectRanch(ranchId) {
      this.oxtallList = [];
      this.where.oxtallId = undefined;
      let that = this;
      OxtallApi.list({"orgId" : ranchId}).then(result => {
        this.oxtallList = (result ? result.data : null) || [];
      }).catch((err) => {
        console.log(err)
      });
    },

  }
};
</script>
